package com.linxz.wandnroid.appui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.webkit.WebView;

import androidx.annotation.Nullable;

import com.linxz.framework.base.AppActivityManager;
import com.linxz.framework.base.BaseStatusBarActivity;
import com.linxz.framework.ui.LinxzWebView;
import com.linxz.ui.widgets.LinxzActionBar;
import com.linxz.wandnroid.R;

/**
 * @author Linxz
 * 创建日期：2019年05月12日 01:02
 * version：v4.0.1
 * 描述：
 */
public class WanAndroidWebViewActivity extends BaseStatusBarActivity {

    private LinxzActionBar actionBar;
    private LinxzWebView webView;
    private String url;
    private boolean loadHtml;

    public static void launch(Activity activity,String url){
        Intent intent=new Intent(activity,WanAndroidWebViewActivity.class);
        intent.putExtra("url",url);
        AppActivityManager.getInstance().goTo(activity,intent);
    }


    public static void launch(Activity activity,String url,boolean loadHtml){
        Intent intent=new Intent(activity,WanAndroidWebViewActivity.class);
        intent.putExtra("url",url);
        intent.putExtra("loadHtml",loadHtml);
        AppActivityManager.getInstance().goTo(activity,intent);
    }


    @Override
    public void onGetBundle(Bundle bundle) {
        super.onGetBundle(bundle);
        url=getIntent().getStringExtra("url");
        loadHtml=getIntent().getBooleanExtra("loadHtml",false);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wan_act_webview);
        initUI();
        setListener();
        initData();
    }

    @Override
    protected void initUI() {
        actionBar=findViewById(R.id.actionBar);
        webView=findViewById(R.id.webView);
    }

    @Override
    protected void setListener() {
        super.setListener();
        webView.setReceivedTitle(new LinxzWebView.ReceivedTitle() {
            @Override
            public void onReceivedTitle(WebView view, String title) {
                actionBar.setTittle(title);

            }
        });
    }

    @Override
    protected void initData() {
        if (loadHtml){
            webView.loadUrl(url);
        }else{
            if (!TextUtils.isEmpty(url) && !"eggFrenzy".equals(url)) {
                webView.loadUrl(url);
            }else if ("eggFrenzy".equals(url)){
                webView.loadUrl("file:////android_asset/eggFrenzy/index.html");
            }
            else{
                webView.loadUrl("file:////android_asset/agreement.htm");
            }
        }
    }

    @Override
    public void onBackPressed() {
        if (!webView.goBack()){
            super.onBackPressed();
        }
    }
}
